package com.csweimei.rentmanagementtreasure.pop;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.core.content.FileProvider;

import com.csweimei.rentmanagementtreasure.BuildConfig;
import com.csweimei.rentmanagementtreasure.R;
import com.csweimei.rentmanagementtreasure.base.AppConfig;
import com.csweimei.rentmanagementtreasure.http.entity.HouseListBean;
import com.csweimei.rentmanagementtreasure.tenant.ui.act.house.AddHzZyHouseActivity;
import com.csweimei.rentmanagementtreasure.tenant.ui.act.house.AddZzZyHouseActivity;
import com.csweimei.rentmanagementtreasure.utils.FileUtils;
import com.csweimei.rentmanagementtreasure.utils.ShapeUtils;
import com.csweimei.rentmanagementtreasure.utils.ValidateUtils;
import com.csweimei.rentmanagementtreasure.utils.permission.RxPermissionsUtils;
import com.lxj.xpopup.core.BottomPopupView;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import me.goldze.mvvmhabit.utils.ToastUtils;

public class PopEditHouse1 extends BottomPopupView {
    private Context mContext;


    private LinearLayout share,add_zy,del,add_state;


    public PopEditHouse1(@NonNull Context context) {
        super(context);
        this.mContext = context;
    }

    ViewGroup viewGroup;
    public void setShareView(ViewGroup view){
        viewGroup = view;
    }

    public PopSelClick mPopSelClick;
    public void setSelBack(PopSelClick popSelClick){
        this.mPopSelClick = popSelClick;
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.pop_edit_house_1;
    }

    @Override
    protected void onCreate() {
        share = findViewById(R.id.share);
        add_zy = findViewById(R.id.add_zy);
        add_state = findViewById(R.id.add_state);


        del = findViewById(R.id.del);
        ImageView cancel = findViewById(R.id.pop_close);
        cancel.setOnClickListener(view -> dismiss());

        add_zy.setOnClickListener(v -> {
            Intent intent;
            if (type.equals("整租")){
                intent = new Intent(mContext, AddZzZyHouseActivity.class);
                intent.putExtra("id",mId);
            }else {
                intent = new Intent(mContext, AddHzZyHouseActivity.class);
                intent.putExtra("id",mId);
                intent.putExtra("roomid",roomId);
            }
            mContext.startActivity(intent);
            dismiss();

        });

        add_state.setOnClickListener(v -> {
            PopWheelBase popWheelBase = new PopWheelBase(mContext, 1, new PopWheelBase.Click() {
                @Override
                public void click(String s) {
                    if (mPopSelClick != null){
                        mPopSelClick.sel(new PopBean(s));
                    }
                }
            });
            List<String> list = new ArrayList<>();
            list.add("空置中");
            list.add("签约中");
            list.add("不可用");
            popWheelBase.setList1(list);
            PopUtils.showPop(mContext,popWheelBase,false);
            dismiss();
        });


        del.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mPopSelClick != null){
                    mPopSelClick.sel(new PopBean("删除"));
                }
                dismiss();
            }
        });

        share.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                RxPermissionsUtils.fileAll((Activity) mContext, new RxPermissionsUtils.Accept() {
                    @Override
                    public void accept() {
                        if (viewGroup != null){

                            saveImageToGallery(mContext,getBitmapFromView(viewGroup));

                        }
                    }
                });
            }
        });
    }

    public Bitmap getBitmapFromView(View view) {
        // 创建和布局相同大小的Bitmap
        Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        canvas.drawColor(Color.WHITE);
        // 将布局绘制在Bitmap上
        view.draw(canvas);
        return bitmap;
    }


    private boolean saveImageToGallery(Context mContext, Bitmap bmp) {
        // 首先保存图片
        String storePath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "zgb";
        String fileName = "zgb_house.jpg";

        FileUtils.createNewDir(storePath);
//        File appDir = new File(storePath);
//        if (!appDir.exists()) {
//            appDir.mkdir();
//        }

        File file = new File(storePath, fileName);
        try {
            FileOutputStream fos = new FileOutputStream(file);
            //通过io流的方式来压缩保存图片
            boolean isSuccess = bmp.compress(Bitmap.CompressFormat.JPEG, 60, fos);
            fos.flush();
            fos.close();

            //把文件插入到系统图库
            //MediaStore.Images.Media.insertImage(context.getContentResolver(), file.getAbsolutePath(), fileName, null);

            //保存图片后发送广播通知更新数据库
            Uri uri = Uri.fromFile(file);
            mContext.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri));
            if (isSuccess) {
                ToastUtils.showShort("保存成功");

                Uri imageUri = Uri.parse(MediaStore.Images.Media.insertImage(mContext.getContentResolver(),
                        bmp, null, null));
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_SEND);
                intent.putExtra(Intent.EXTRA_STREAM, imageUri);
                intent.setType("image/*");
                mContext.startActivity(Intent.createChooser(intent, "分享到"));

                viewGroup.destroyDrawingCache();
                viewGroup.setDrawingCacheEnabled(false);
                dismiss();
                return true;
            } else {
                ToastUtils.showShort("保存失败");
                return false;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }



    public String mId = "0";
    public void setSelId(String id) {
        mId = id;
    }

    public String roomId = "0";
    public void setRoomId(String id) {
        roomId = id;
    }

    public String type = "整租";
    public void setSelType(String id) {
        type = id;
    }
}
